using System.Timers;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Rendering.UI;
using UnityEngine.UI;

namespace Hydrogen.ULib.UINavigation
{
    [RequireComponent(typeof(UINavNode))]
    [RequireComponent(typeof(ScrollRect))]
    public class UIScrollRect : UISelectableBase,
        IUINavNodeMovedHandle,
        IBeginDragHandler,
        IEndDragHandler
    {
        protected override Color GetFKColor() => Color.blue;


        private ScrollRect _sr
        {
            get
            {
                if (!m_sr) m_sr = GetComponent<ScrollRect>();
                return m_sr;
            }
        }
        private ScrollRect m_sr;



        private void Start()
        {
            if (_sr)
            {
                var node = GetComponent<UINavNode>();
                Arrow arrow = Arrow.None;
                if (_sr.horizontal) arrow |= Arrow.Left | Arrow.Right;
                if (_sr.vertical) arrow |= Arrow.Up | Arrow.Down;
                node.MoveableArrow = ~arrow;
            }
        }

        private static Vector2 _delta = new Vector2(0.1f, 0.1f);
        private Vector2 _targetNormaledPos;
        public void OnMoved(Arrow arrow, IUINavSelectable next)
        {
            switch (arrow)
            {
                case Arrow.Left:
                    if (_targetNormaledPos.x > _delta.x)
                        _targetNormaledPos += Vector2.left * _delta.x;
                    else _targetNormaledPos += Vector2.left * _targetNormaledPos.x;
                    break;
                case Arrow.Right:
                    if (_targetNormaledPos.x < 1 - _delta.x)
                        _targetNormaledPos += Vector2.right * _delta.x;
                    else _targetNormaledPos += Vector2.right * (1 - _targetNormaledPos.x);
                    break;
                case Arrow.Up:
                    if (_targetNormaledPos.y < 1 - _delta.y)
                        _targetNormaledPos += Vector2.up * _delta.y;
                    else _targetNormaledPos += Vector2.up * (1 - _targetNormaledPos.y);
                    break;
                case Arrow.Down:
                    if (_targetNormaledPos.y > _delta.y)
                        _targetNormaledPos += Vector2.down * _delta.y;
                    else _targetNormaledPos += Vector2.down * _targetNormaledPos.y;
                    break;
            }
        }

        [SerializeField]
        private float _lerpSpeed = 10;
        private void Update()
        {
            if (!_isDraging && _sr)
            {
                _sr.normalizedPosition = Vector2.Lerp(_sr.normalizedPosition, _targetNormaledPos, _lerpSpeed * Time.deltaTime);
            }
        }

        [SerializeField]
        private bool _isDraging;
        void IBeginDragHandler.OnBeginDrag(PointerEventData eventData)
        {
            this._isDraging = true;
        }
        void IEndDragHandler.OnEndDrag(PointerEventData eventData)
        {
            this._isDraging = false;
            this._targetNormaledPos = _sr.normalizedPosition;
            this._targetNormaledPos.x = Mathf.Clamp01(this._targetNormaledPos.x);
            this._targetNormaledPos.y = Mathf.Clamp01(this._targetNormaledPos.y);
            
        }
    }
}
